package com.wei.czz.common.utils;

import com.alibaba.fastjson2.JSON;
import com.wei.czz.common.enums.ResultEnum;
import com.wei.czz.common.tool.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Optional;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2021-03-15 00:00:00
 * className: HttpContextUtils Http上下文工具类
 * version: 1.0
 * description:
 */
public class HttpUtils {

	private static final Logger log = LoggerFactory.getLogger(HttpUtils.class);

	/**
	 * 获取当前请求的http请求对象
	 * @return http请求对象
	 */
	public static HttpServletRequest getHttpServletRequest() {
		return Optional.ofNullable(RequestContextHolder.getRequestAttributes())
				.map(requestAttributes -> (ServletRequestAttributes) requestAttributes)
				.map(ServletRequestAttributes::getRequest)
				.orElse(null);
	}

	/**
	 * 获取当前请求的http结果对象
	 * @return http结果对象
	 */
	public static HttpServletResponse getHttpServletResponse() {
		return Optional.ofNullable(RequestContextHolder.getRequestAttributes())
				.map(requestAttributes -> (ServletRequestAttributes) requestAttributes)
				.map(ServletRequestAttributes::getResponse)
				.orElse(null);
	}

	/**
	 * 设置http请求返回结果
	 * @param resultEnum 请求结果枚举对象
	 */
	public static void setResponse(ResultEnum resultEnum) {
		HttpServletRequest request = getHttpServletRequest();
		HttpServletResponse response = getHttpServletResponse();

		setResponse(request, response, resultEnum.getCode(), resultEnum.getMsg());
	}

	/**
	 * 设置http请求返回结果
	 * @param code     状态码
	 * @param message  提示消息
	 */
	public static void setResponse(HttpServletRequest request, HttpServletResponse response, Integer code,
								   String message) {
		try {
			// 重置response
			response.reset();
			response.setStatus(ResultEnum.SUCCESS.getCode());
			// 设置结果类型
			response.setContentType("application/json;charset=utf-8");
			response.setHeader("Access-Control-Allow-Credentials", "true");
			response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
			// 设置编码类型
			response.setCharacterEncoding("utf-8");
			Result<Object> result = Result.getResult(code, message);
			response.getWriter().println(JSON.toJSONString(result));
		} catch (IOException e) {
			log.error("请求响应结果输出异常。message={}", e.getMessage(), e);
		}
	}

	/**
	 * 设置http请求返回结果
	 * @param request  http请求对象
	 * @param response http结果对象
	 * @param code     状态码
	 * @param message  提示消息
	 * @param data     返回数据
	 */
	public static void setResponse(HttpServletRequest request, HttpServletResponse response, Integer code,
								   String message, Object data) {
		try {
			// 重置response
			response.reset();
			response.setStatus(ResultEnum.SUCCESS.getCode());
			// 设置结果类型
			response.setContentType("application/json;charset=utf-8");
			response.setHeader("Access-Control-Allow-Credentials", "true");
			response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
			// 设置编码类型
			response.setCharacterEncoding("utf-8");
			Result<Object> result = Result.getResult(code, message, data);
			response.getWriter().println(JSON.toJSONString(result));
		} catch (IOException e) {
			log.error("请求响应结果输出异常。message={}", e.getMessage(), e);
		}
	}

	/**
	 * 设置http请求返回结果
	 * @param request  http请求对象
	 * @param response http结果对象
	 * @param result   结果对象
	 */
	public static <T> void setResponse(HttpServletRequest request, HttpServletResponse response, Result<T> result) {
		try {
			// 重置response
			response.reset();
			response.setStatus(ResultEnum.SUCCESS.getCode());
			// 设置结果类型
			response.setContentType("application/json;charset=utf-8");
			response.setHeader("Access-Control-Allow-Credentials", "true");
			response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
			// 设置编码类型
			response.setCharacterEncoding("utf-8");
			response.getWriter().println(JSON.toJSONString(result));
		} catch (IOException e) {
			log.error("请求响应结果输出异常。message={}", e.getMessage(), e);
		}
	}

}
